package com.yunnuo.sysmonitor.controller;


import com.yunnuo.common.controller.BaseController;
import com.yunnuo.common.web.ResultData;
import com.yunnuo.common.web.page.TableDataInfo;
import com.yunnuo.sysmonitor.domain.OnlineSession;
import com.yunnuo.sysmonitor.domain.UserOnline;
import com.yunnuo.sysmonitor.service.IUserOnlineService;
import com.yunnuo.system.shiro.session.OnlineSessionDAO;
import net.sf.json.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 在线用户监控
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/sysmonitor/online")
public class UserOnlineController extends BaseController
{
    private String prefix = "sysmonitor/online";

    @Autowired
    private IUserOnlineService userOnlineService;

    @Autowired
    private OnlineSessionDAO onlineSessionDAO;

    @RequiresPermissions("sysmonitor:online:view")
    @GetMapping()
    public String online()
    {
        return prefix + "/online";
    }

    @RequiresPermissions("sysmonitor:online:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserOnline userOnline)
    {
        startPage();
        List<UserOnline> list = userOnlineService.selectUserOnlineList();
        return getDataTable(list);
    }

    @RequiresPermissions("sysmonitor:online:batchForceLogout")
    @PostMapping("/batchForceLogout")
    @ResponseBody
    public JSON batchForceLogout(@RequestParam("ids[]") String[] ids)
    {
        ResultData data = new ResultData();
        for (String sessionId : ids)
        {
            UserOnline online = userOnlineService.selectOnlineById(sessionId);
            if (online == null)
            {
                data.setData("");
                data.setStat(ResultData.STAT_FAILURE);
                data.setMessage("用户已下线！");
            }
            OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
            if (onlineSession == null)
            {
                data.setData("");
                data.setStat(ResultData.STAT_FAILURE);
                data.setMessage("用户已下线！");
            }
            onlineSession.setStatus(OnlineSession.OnlineStatus.off_line);
            online.setStatus(OnlineSession.OnlineStatus.off_line);
            userOnlineService.saveOnline(online);
            data.setData("");
            data.setStat(ResultData.STAT_SUCCESS);
            data.setMessage("操作成功！");
        }

        return data.toJson();
    }

    @RequiresPermissions("sysmonitor:online:forceLogout")
    @RequestMapping("/forceLogout/{sessionId}")
    @ResponseBody
    public JSON forceLogout(@PathVariable("sessionId") String sessionId) {
        ResultData data = new ResultData();
        UserOnline online = userOnlineService.selectOnlineById(sessionId);
        if (online == null)
        {
            data.setData("");
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("用户已下线！");
        }
        OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
        if (onlineSession == null)
        {
            data.setData("");
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("用户已下线！");
        }
        onlineSession.setStatus(OnlineSession.OnlineStatus.off_line);
        online.setStatus(OnlineSession.OnlineStatus.off_line);
        userOnlineService.saveOnline(online);
        data.setData("");
        data.setStat(ResultData.STAT_SUCCESS);
        data.setMessage("操作成功！");

        return data.toJson();
    }

}
